$(document).ready(function() {
    handleCheckedBox();
    handleSelectedBox();
    linkToggleHandle();
});

function linkToggleHandle() {
    $('.title-toggle').click(function() {
        $(this).next('.content-toggle').stop(true, true).slideToggle(200);
    });
}

function handleCheckedBox() {
    $('.checkbox').find('i, span').remove();
    $('.checkbox').each(function() {
        var _this = this;
        var text = $(_this).text();
        $(_this).append('<i>');
        $(_this).append('<span>' + text + '</span>');

        var $checkbox = $(_this).find('input[type="checkbox"]');
        var checked = $checkbox.is(':checked');
        checked ? $(_this).find('i').addClass('checked') : '';

    });

    $('.checkbox').not('.disabled').delegate('> input[type="checkbox"]', 'click', function() {
        var checked = $(this).is(':checked');
        checked ? $(this).siblings('i').addClass('checked') : $(this).siblings('i').removeClass('checked');
    });

}

function handleSelectedBox() {
    $('.selectedbox').find('label, ul').remove();
    $('.selectedbox').each(function() {
        var _this = this;
        var $sel = $(_this).find('select');
        var $opt = $sel.find('option');
        var selectedVal = $sel.find(':selected').val();
        var selected = $sel.find(':selected').text();

        $(_this).append('<label>');
        $(_this).append('<ul>');

        $opt.each(function() {
            var val = $(this).val();
            var text = $(this).text();
            $(_this).find('>label').text(selected);
            var classActive = (selectedVal === val) ? 'actived' : '';
            $(_this).find('>ul').append('<li data-value="' + val + '" class="' + classActive + '" >' + text + '</li>');
        });
    });

    $('.selectedbox').delegate('label', 'click', function() {
        $(this).siblings('ul').slideDown(200);
    });

    var $selectedbox = $(".selectedbox");
    $(window).on("click.Bst", function(event) {
        if ($selectedbox.has(event.target).length === 0 && !$selectedbox.is(event.target)) {
            $selectedbox.find('ul').slideUp(200);
        } else {

        }
    });

    $('.selectedbox').delegate('>ul >li', 'click', function() {
        var val = $(this).data('value');
        var text = $(this).text();
        $(this).closest('.selectedbox').find('>ul >li').removeClass('actived');
        $(this).addClass('actived');
        $(this).parent('').siblings('label').text(text);
        $(this).closest('.selectedbox').find('select option').removeAttr('selected');
        $(this).parent('ul').siblings('select').find('option[value="' + val + '"]').attr('selected', 'selected');
        $(this).parent('ul').slideUp(200);
    });

}


var messagebox = function() {
};

messagebox.prototype = {
    createloadingTable1: function(table) {
        $(table).prepend('<div id="tableLoading1"></div>');
        //$(table).css({position: "relative"});
    },
    destroyloadingTable1: function() {
        $('#tableLoading1').remove();
    }
};



function validateRequired($selector, msg) {
    if ($selector.val() === "") {
        $selector.siblings('.notification').show().text(msg);
        return false;
    } else {
        $selector.siblings('.notification').show().text('');
        return true;
    }
}

function validateMatch($selector1, $selector2, msg) {
    if ($selector1.val() !== $selector2.val()) {
        $selector2.siblings('.notification').show().text(msg);
        return false;
    } else {
        $selector2.siblings('.notification').show().text('');
        return true;
    }
}

function validateNumber($selector, msg) {
    var regex = /^\d+$/;
    if (regex.test($selector.val()) === false) {
        $selector.siblings('.notification').show().text(msg);
        return false;
    } else {
        $selector.siblings('.notification').show().text('');
        return true;
    }
}

function generateString(string, length) {
    var result = "";
    for (i = 0; i < length; i++) {
        var rand = Math.floor((Math.random() * string.length));
        result += string.charAt(rand);
    }
    return result;
}

// Numeric only control handler
jQuery.fn.ForceNumericOnly = function() {
    return this.each(function() {
        $(this).keydown(function(e) {
            var key = e.charCode || e.keyCode || 0;
            return (key === 8 || key === 9 || key === 13 || key === 46 || key === 110 || key === 190
                    || (key >= 35 && key <= 40)
                    || (key >= 48 && key <= 57)
                    || (key >= 96 && key <= 105));
        });
    });
};


function openDialogMessage(title, content) {
    $('body').prepend('<div id="dialogMessage" title="' + title + '"><p class="content">' + content + '</p></div>');
    $("#dialogMessage").dialog({
        autoOpen: true,
        modal: true,
        width: 400,
        height: 200,
        buttons: {
            OK: function() {
                $(this).dialog("destroy");
                $(this).remove();
            }
        }
    });
}

function goBack() {
    window.history.back();
}